const jwt = require('jsonwebtoken')

let cert = `xxxxxxxx`

class Jwt {
    constructor(data, minute) {
        this.data = data;
        this.minute = minute || 60 * 12
    }

    getToken() {
        let data = this.data;
        return jwt.sign(
            {
                data
            },
            cert,
            {
                algorithm: "HS256",
                issuer: "one",
                expiresIn: this.minute * 60,
            }
        );
    }

    verifyToken() {
        let token = this.data
        try {
            let info = jwt.verify(token, cert, {
                issuer: "one",
                algorithms: ["HS256"],
            });

            return {
                info,
                code: 200
            };
        } catch (error) {
            return {
                code: -1,
                msg: '校验token失败,请重新登录。'
            }
        }
    }
}
module.exports = Jwt;